package com.xxx.yuedong.mine.service.impl;

import cn.hutool.core.map.MapUtil;
import com.xxx.yuedong.common.R;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxx.yuedong.mine.db.dao.UserDao;
import com.xxx.yuedong.mine.pojo.User;
import com.xxx.yuedong.mine.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public ArrayList<String> searchUserPermission(Long id) {
        ArrayList<String> list = userDao.searchUserPermission(id);
        return list;
    }

    @Override
    public int insert(User user) {
        int rows = userDao.insert(user);
        return rows;
    }

    @Override
    public HashMap login(HashMap param) {
        HashMap map = userDao.login(param);
        return map;
    }

    @Override
    public Integer updatePoints(HashMap param) {
        Integer rows = userDao.updatePoints(param);
        return rows;
    }

    @Override
    public HashMap searchUserInfo(Long userId) {
        HashMap map = userDao.searchUserInfo(userId);
        return map;
    }

    @Override
    public List<HashMap> searchAllUserInfo() {
        List<HashMap> allUserInfo = userDao.searchAllUserInfo();
        return allUserInfo;
    }

    @Override
    public boolean searchUserByUsername(String username) {
        return userDao.searchUserByUsername(username) != 0 ? true : false;
    }

    @Override
    public int insertGuidePageData(HashMap param) {
        int rows = userDao.insertGuidePageData(param);

        return rows;
    }

    @Override
    public R searchAllUserByPage(HashMap param) {
        Integer pageIndex = MapUtil.getInt(param, "pageIndex");
        Integer length = MapUtil.getInt(param, "pageSize");
        Integer start = (pageIndex - 1) * length;

        param.put("start",start);
        param.put("length",length);

        ArrayList<HashMap> list = userDao.searchAllUserByPage(param);
        Integer count = userDao.searchAllUserCount(param);

        return R.ok()
                .put("list",list)
                .put("totalCount",count)
                .put("pageIndex",pageIndex)
                .put("length",length);

    }

    @Override
    public R update(HashMap param) {
        Integer rows = userDao.updateAdmin(param);
        if (rows == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    public int addPoints(long userId, int pointsToAdd) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", userId); // 指定更新条件为id等于传入的userId
        updateWrapper.setSql("points = points + " + pointsToAdd); // 更新points字段为原值加上传入的pointsToAdd

        return userDao.update(null, updateWrapper);
    }

    @Override
    public Integer searchUserPoints(Long id) {
        Integer rows = userDao.searchUserPoints(id);
        return rows;
    }
}
